#coding=utf-8
"""
進入爱逛卖家版
"""
import time

import yaml
from uiautomator2 import xpath

from page_object.add_market_page import AddGoodsPage
from page_object.base_page import BasePage
from page_object.guang_page import GuangPage
from page_object.preview_page import PreviewPage
from page_object.setting_page import SettingPage
from page_object.stock_page import StockPage
from resource.path_info import *

"""
橱窗管理
"""
class ShopWinPage(GuangPage):


    def get_sell_goods_list(self, d):
        print("getSellGoodsList")
        nameLists = []
        for i in range(1,5,1):
            if d.xpath(goodsTitle_xpath %i).exists:
                nameLists.append(d.xpath(goodsTitle_xpath %i).get_text()[8:])
        print(nameLists)
        return nameLists

    def to_add_goods_page(self, d):
        print("to add goods page")
        if d.xpath(living_next1_xpath).exists:
            d.xpath(living_next1_xpath).click()
            d.xpath(living_next2_xpath).click()
            d.xpath(living_next3_xpath).click()
        d.xpath(addGoods_but_xpath).click()
        return AddGoodsPage()

    def to_goods_preview_page(self, d):
        print("to_goods_preview_page")
        d.xpath(preview_but_xpath).click()
        return PreviewPage()

    def returnPreviewPage(self, d):
        return PreviewPage()

    def check_add_good(self, d, goodTitle, index):
        print("check Added Good Title")
        assert goodTitle == d.xpath(goodsTitle_xpath %index).get_text()[8:]
        return self

    def move_market_goods(self, d, goodTitle, index):
        d.xpath(move_market_goods_but_xpath % index).click()
        time.sleep(1)
        # if d.xpath(goodsTitle_xpath % index).exists:
        #     assert not goodTitle == d.xpath(goodsTitle_xpath % index).get_text()[8:]
        return self

    def move_self_goods(self, d, goodTitle, index):
        d.xpath(move_self_goods_but_xpath % index).click()
        time.sleep(1)
        # if d.xpath(goodsTitle_xpath % index).exists:
        #     assert not goodTitle == d.xpath(goodsTitle_xpath % index).get_text()[8:]
        return self


    def to_stock_page(self, d):
        print("to add goods page")
        d.xpath(stock_tab_xpath).click()
        return StockPage()

    def delete_self_goods(self, d, goodTitle, index):
        if goodTitle == self.get_shop_good_name(d, index):
            d.xpath(delete_self_xpath % index).click()
            time.sleep(1)
            if d.xpath(goodTitle_xpath % index).exists:
                assert goodTitle != self.get_shop_good_name(d, index)
        else:
            for i in range(1, 5, 1):
                if goodTitle == self.get_shop_good_name(d, i):
                    time.sleep(1)
                    d.xpath(delete_self_xpath % i).click()
                    time.sleep(1)
                    if d.xpath(goodTitle_xpath % index).exists:
                        assert goodTitle != self.get_shop_good_name(d, i)
        time.sleep(2)
        d.press("back")
        time.sleep(1)
        if d.xpath(addGoods_but_xpath).exists:
            d.press("back")
        return self

    def delete_market_goods(self, d, goodTitle, index):
        if goodTitle == self.get_shop_good_name(d, index):
            d.xpath(delete_market_xpath % index).click()
            time.sleep(1)
            if d.xpath(goodTitle_xpath % index).exists:
                assert goodTitle != self.get_shop_good_name(d, index)
        else:
            for i in range(1, 5, 1):
                if goodTitle == self.get_shop_good_name(d, i):
                    d.xpath(delete_market_xpath % i).click()
                    time.sleep(1)
                    if d.xpath(goodTitle_xpath % index).exists:
                        assert goodTitle != self.get_shop_good_name(d, i)
        time.sleep(2)
        d.press("back")
        time.sleep(1)
        if d.xpath(addGoods_but_xpath).exists:
            d.press("back")
        return self

    def get_shop_good_name(self, d, index):
        shopGoodsName = d.xpath(goodsTitle_xpath % index).get_text()[8:]
        print("get shop win good name:%s" % shopGoodsName)
        return shopGoodsName